<?php

namespace App\Http\Controllers\Api;

use App\CompanyInfo;
use App\EducationExperience;
use App\EduWorkLabel;
use App\JobType;
use App\Position;
use App\PositionImg;
use App\PositonResume;
use App\Resume;
use App\SkillLabel;
use App\UserFollow;
use App\WorkExperience;
use http\Env\Response;
use Illuminate\Http\Request;
use App\Http\Services\FileManager;
use App\Http\Services\UserManager;
use App\PersonalAuth;
use App\User;
use App\Userinfo;
use App\Trade;
use App\Message;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\Types\Array_;


class UserCenterController extends Controller
{
    public function getUserInfo(Request $request){
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录！',"data"=>"no_login"]);

        $userInfo = UserInfo::whereUserId($user->id)->first();
        if(!$userInfo)
            return response()->json(['level' => 'error','message' => '用户信息不存在！']);

        $userInfo->photo_url = FileManager::getImgFromOssUrl($userInfo->photo_id);

        $user->user_info = $userInfo;

        return response()->json(['level' => 'success','message' => '获取成功','data' => $user]);
    }



    public function setUserInfo(Request $request){
        $photoId        = $request->get('phone_id');
        $realname       = $request->get('realname');
        $nickName       = $request->get('nickname');
        $sex            = $request->get('sex');
        $birth          = $request->get('birth');
        $province       = $request->get('province');
        $city           = $request->get('city');
        $area           = $request->get('area');
        $addressDetail  = $request->get('address_detail');
        $tag            = $request->get('tag');
        $integral       = $request->get('integral');
        $skill          = $request->get('skill');

        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $userInfo = UserInfo::whereUserId($user->id)->first();
        if(!$userInfo)
            $userInfo = new UserInfo();

        $userInfo->user_id = $user->id;
        if(!is_null($photoId))$userInfo->photo_id = $photoId;
        if(!is_null($realname))$userInfo->realname = $realname;
        if(!is_null($nickName))$userInfo->nickname = $nickName;
        if(!is_null($sex))$userInfo->sex = $sex;
        if(!is_null($birth))$userInfo->birth = $birth;
        if(!is_null($province))$userInfo->province = $province;
        if(!is_null($city))$userInfo->city = $city;
        if(!is_null($area))$userInfo->area = $area;
        if(!is_null($addressDetail))$userInfo->address_detail = $addressDetail;
        if(!is_null($tag))$userInfo->tag = $tag;
        if(!is_null($integral))$userInfo->integral = $integral;
        if(!is_null($skill))$userInfo->skill = $skill;
        $userInfo->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $userInfo]);
    }

    public  function getCompanyInfo(Request $request){
        $user=UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level'=>'error','message'=>'请先登录','data'=>'no_login']);
        $companyInfo=CompanyInfo::whereUserId($user->id)->first();
        if(!$companyInfo)
            return response()->json(['level'=>'error','message'=>'企业资料未填写']);

        $trade = Trade::whereId($companyInfo->industry_id)->first();
        if($trade){
            $companyInfo->industry = $trade->name;
        }else{
            $companyInfo->industry = null;
        }

        $companyInfo->photo_url = FileManager::getImgFromOssUrl($companyInfo->photo_id);
        $companyInfo->license_img_url = FileManager::getImgFromOssUrl($companyInfo->license_img_id);

        return response()->json(['level'=>'success','message'=>'获取成功','data'=>$companyInfo]);
    }

    public function setCompanyInfo(Request $request){
        $name             =$request->get('name');
        $province         =$request->get('province');
        $city             =$request->get('city');
        $area             =$request->get('area');
        $address_detail   =$request->get('address_detail');
        $industryId       =$request->get('industry_id');
        $size             =$request->get('size');
        $introduction     =$request->get('introduction');
        $contacts         =$request->get('contacts');
        $contact_phone    =$request->get('contact_phone');
        $photo_id         =$request->get('photo_id');

        $user=UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录！',"data"=>"no_login"]);
        $companyInfo=CompanyInfo::whereUserId($user->id)->first();
        if (!$companyInfo)
            $companyInfo=new companyInfo();

            $companyInfo->user_id=$user->id;
        if (!is_null($name))$companyInfo->name=$name;
        if (!is_null($province))$companyInfo->province=$province;
        if (!is_null($city))$companyInfo->city=$city;
        if (!is_null($area))$companyInfo->area=$area;
        if (!is_null($address_detail))$companyInfo->address_detail=$address_detail;
        if (!is_null($industryId))$companyInfo->industry_id=$industryId;
        if (!is_null($size))$companyInfo->size=$size;
        if(!is_null($introduction))$companyInfo->introduction=$introduction;
        if (!is_null($contacts))$companyInfo->contacts=$contacts;
        if (!is_null($contact_phone))$companyInfo->contact_phone=$contact_phone;
        if (!is_null($photo_id))$companyInfo->photo_id=$photo_id;

        $companyInfo->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $companyInfo]);

    }

    public function setCompanyAuthInfo(Request $request){
        $name              = $request->get("name");
        $phone             = $request->get("contact_phone");
        $legal_person_name = $request->get('legal_person_name');
        $license           = $request->get('license');
/*        $license_img_id    = is_null($request->get('license_img_id'))?$request->get('license_img_id'):'';*/

        $license_img_id=$request->get('license_img_id');


        /** @var User $user */
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(["level"=>"error","message"=>"未登录","data"=>"no_login"]);

        /** @var Userinfo $userInfo */
        $userInfo = Userinfo::whereUserId($user->id)->first();
        if(!$userInfo)
            return response()->json(["level"=>"error","message"=>"个人信息不完整"]);

        if(is_null($name) || $name == '')
            return response()->json(["level"=>"error","message"=>"公司名称不能为空"]);

        if(is_null($phone) || $phone == '')
            return response()->json(["level"=>"error","message"=>"联系方式不能为空"]);

        if(is_null($legal_person_name) || $legal_person_name == '')
            return response()->json(["level"=>"error","message"=>"法人姓名不能为空"]);

        if(is_null($license) || $license == '')
            return response()->json(["level"=>"error","message"=>"统一信用代码不能为空"]);

        if(is_null($license_img_id) || $license_img_id == ''||$license_img_id==0)
            return response()->json(["level"=>"error","message"=>"请上传营业执照!"]);

        /** @var CompanyInfo $thatCompanInfo */
        $thatCompanInfo = CompanyInfo::whereUserId($user->id)->first();
        if(!$thatCompanInfo)
            return response()->json(["level"=>"error","message"=>"企业信息不存在"]);

        $thatCompanInfo->name = $name;
        $thatCompanInfo->contact_phone = $phone;
        $thatCompanInfo->legal_person_name = $legal_person_name;
        $thatCompanInfo->license           = $license;
        $thatCompanInfo->license_img_id    = $license_img_id;
        $thatCompanInfo->save();

        $userInfo->company_auth = 2;
        $userInfo->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $thatCompanInfo]);
    }

    public function setResume(Request $request){
        $realname       = $request->get('realname');
        $sex            = $request->get('sex');
        $age            = $request->get('age');
        $phone          = $request->get('phone');
        $photo_id       = $request->get('photo_id');
        $education      = $request->get('education');
        $skill          = $request->get('skill');
        $province       = $request->get('province');
        $city           = $request->get('city');
        $area           = $request->get('area');
        $work_nature    = $request->get('work_nature');
        $job_type_id    = $request->get('job_type_id');
        $expected_salary_type = $request->get('expected_salary_type');
        $expected_salary      = $request->get('expected_salary');

        /** @var Userinfo $user */
        $user=UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录！',"data"=>"no_login"]);

        $userInfo = Userinfo::whereUserId($user->id)->first();
        if(!$userInfo)
            return response()->json(['level' => 'error','message' => '用户数据错误！']);

        if(!is_null($sex))$userInfo->sex=$sex;
        if(!is_null($photo_id))$userInfo->photo_id=$photo_id;
        if(!is_null($skill))$userInfo->skill=$skill;
        if(!is_null($realname))$userInfo->realname=$realname;
        $userInfo->save();

        $newResume=Resume::whereUserId($user->id)->first();

        if (!$newResume)
            $newResume = new  Resume();
            $newResume->user_id = $user->id;
            if (!is_null($age))$newResume->age = $age;
            if (!is_null($phone))$newResume->phone = $phone;
            if (!is_null($education))$newResume->education = $education;
            if (!is_null($province))$newResume->province = $province;
            if (!is_null($city))$newResume->city = $city;
            if (!is_null($area))$newResume->area = $area;
            if (!is_null($work_nature))$newResume->work_nature = $work_nature;
            if(!is_null($expected_salary_type))$newResume->expected_salary_type=$expected_salary_type;
            if (!is_null($expected_salary))$newResume->expected_salary = $expected_salary;
            if(!is_null($job_type_id))$newResume->job_type_id = $job_type_id;

        $newResume->save();

        $result = array(
            "user_info" => $userInfo,
                   "resume"    => $newResume
        );

        return response()->json(['level' => 'success','message' => '设置成功','data' => $result]);
    }

    public function addResume(Request $request){

        $type                       =$request->get('type');
        $type_id                       =!is_null($request->get('type_id'))?$request->get('type_id'):0;
        //todo 基本资料
        $realname                   = $request->get('realname');
        $sex                        = $request->get('sex');
        $age                        = $request->get('age');
        $phone                      = $request->get('phone');
        $photo_id                   = $request->get('photo_id');
        $education                  = $request->get('education');
        $skill                      = $request->get('skill');
        $job_status                 =$request->get('job_status');
        $work_experience_num        =$request->get('work_experience_num');


        //todo 求职期望

        $province                   = $request->get('province');
        $city                       = $request->get('city');
        $area                       = $request->get('area');
        $work_nature                = $request->get('work_nature');
        $job_type_id                = $request->get('job_type_id');
        $expected_salary_type       = $request->get('expected_salary_type');
        $expected_salary            = $request->get('expected_salary');



        //todo 教育经历
        $school_name                =$request->get('school_name');
        $professional_name          =$request->get('professional_name');
        $education_name             =$request->get('education_name');
        $education_start_time       =$request->get('education_start_time');
        $education_end_time         =$request->get('education_end_time');
        $education_desc             =$request->get('education_desc');


        //todo 工作经验

        $work_name                  =$request->get('work_name');
        $work_job_title             =$request->get('work_job_title');
        $work_start_time            =$request->get('work_start_time');
        $work_end_time              =$request->get('work_end_time');
        $work_desc                  =$request->get('work_desc');


        //todo 技能标签
        $label_id                   =$request->get('label_id');


        /** @var Userinfo $user */
        $user=UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录！',"data"=>"no_login"]);

        $userInfo = Userinfo::whereUserId($user->id)->first();
        if(!$userInfo)
            return response()->json(['level' => 'error','message' => '用户数据错误！']);


        if($type=="baisc_data"){



            if(is_null($sex)|| $sex=="")
                return response()->json(['level' => 'error','message' => '请选择性别！']);

            if(is_null($photo_id)||$photo_id=="")
                return response()->json(['level' => 'error','message' => '请上传头像！']);

            if(is_null($realname)||$realname==""||preg_match('/\d/is',$realname)||strlen($realname)>12)
                return response()->json(['level' => 'error','message' => '姓名个数不正确！']);


            $userInfo = Userinfo::whereUserId($user->id)->first();
            if(!$userInfo)
                return response()->json(['level' => 'error','message' => '用户数据错误！']);

            $userInfo->sex=$sex;
            $userInfo->photo_id=$photo_id;
            $userInfo->skill=$skill;
            $userInfo->realname=$realname;
            $userInfo->save();


            if(!preg_match("/^1[345789]{1}\d{9}$/",$phone))

                return response()->json(['level' => 'error','message' => '手机号码格式不正确！']);

            $newResume=Resume::whereUserId($user->id)->first();
            if (!$newResume)
                $newResume = new  Resume();


            $newResume->user_id = $user->id;
            if(!is_null($age))$newResume->age = $age;
            $newResume->phone = $phone;
            if(!is_null($education))$newResume->education = $education;
            if(!is_null($work_experience_num))$newResume->work_experience_num=$work_experience_num;
            $newResume->save();


            $result['baisc_data'] = array(
                "user_info" => $userInfo,
                "resume"    => $newResume
            );


        }
        if($type=="job_intention"){


            $newResume=Resume::whereUserId($user->id)->first();
            if (!$newResume)
                $newResume = new  Resume();

            if (!is_null($job_status))$newResume->job_status=$job_status;
            if(!is_null($province))$newResume->province = $province;
            if(!is_null($city))$newResume->city = $city;
            if(!is_null($area))$newResume->area = $area;
            if(!is_null($work_nature))$newResume->work_nature = $work_nature;
            if(!is_null($expected_salary_type))$newResume->expected_salary_type=$expected_salary_type;
            if(!is_null($expected_salary))$newResume->expected_salary = $expected_salary;
            if(!is_null($job_type_id))$newResume->job_type_id = $job_type_id;

            $newResume->save();

            $result['job_intention']=$newResume;

        }

        //todo 教育经历
        if($type=="education_experience"){

            if(is_null($school_name)||$school_name=="")
                return response()->json(['level' => 'error','message' => '请填写学校名称！']);

            if(is_null($professional_name)||$professional_name=="")
                return response()->json(['level' => 'error','message' => '请输入专业名称！']);

            if(is_null($education_name)||$education_name=="")
                return response()->json(['level' => 'error','message' => '请选择最高学历！']);

            if(is_null($education_start_time)||$education_start_time=="")
                return response()->json(['level' => 'error','message' => '请选择开始时间！']);


            if(is_null($education_end_time)||$education_end_time=="")
                return response()->json(['level' => 'error','message' => '请选择结束时间！']);




            $edu=EduWorkLabel::whereType('edu')->whereTypeId($type_id)->first();
            if(!$edu){

                $educationExperience=new EducationExperience();
                $educationExperience->user_id=$user->id;
                $educationExperience->name=$education_name;
                $educationExperience->professional_name=$professional_name;
                $educationExperience->education=$education_name;
                $educationExperience->start_time=$education_start_time;
                $educationExperience->end_time=$education_end_time;
                $educationExperience->desc=$education_desc;
                $educationExperience->save();

                $newEdu=new EduWorkLabel();
                $newEdu->user_id=$user->id;
                $newEdu->type="edu";
                $newEdu->type_id=$educationExperience->id;
                $newEdu->save();
            }else{

                $educationExperience=EducationExperience::whereId($edu->type_id)->first();
                $educationExperience->user_id=$user->id;
                $educationExperience->name=$education_name;
                $educationExperience->professional_name=$professional_name;
                $educationExperience->education=$education_name;
                $educationExperience->start_time=$education_start_time;
                $educationExperience->end_time=$education_end_time;
                $educationExperience->desc=$education_desc;
                $educationExperience->save();
            }

            $result['education_experience']=$educationExperience;
        }

        //todo 工作经历
        if($type=="work_experience"){

            if(is_null($work_name)||$work_name=="")
                return response()->json(['level' => 'error','message' => '请输入公司名称！']);

            if(is_null($work_job_title)||$work_job_title=="")
                return response()->json(['level' => 'error','message' => '请输入职位名称！']);

            if(is_null($work_start_time)||$work_start_time=="")
                return response()->json(['level' => 'error','message' => '请选择开始时间！']);

            if(is_null($work_end_time)||$work_end_time=="")
                return response()->json(['level' => 'error','message' => '请选择结束时间！']);



            $work=EduWorkLabel::whereType('work')->whereTypeId($type_id)->first();
            if(!$work){

                $workExperience=new WorkExperience();
                $workExperience->user_id=$user->id;
                $workExperience->name=$work_name;
                $workExperience->job_title=$work_job_title;
                $workExperience->start_time=$work_start_time;
                $workExperience->end_time=$work_end_time;
                $workExperience->desc=$work_desc;
                $educationExperience->save();

                $newWork=new EduWorkLabel();
                $newWork->user_id=$user->id;
                $newWork->type="work";
                $newWork->type_id=$type_id;
                $newWork->save();
            }

            $result['work_experience']=$workExperience;


        }
        if($type=="skill_label"){

            if(is_array($type_id)){
                $label=EduWorkLabel::whereUserId($user->id)->whereType("label")->first();
                $type_id=implode(",",$type_id);
                if(!$label)
                    $label=new EduWorkLabel();
                       $label->type_id=$type_id;
                       $label->save();
            }
            $result['skill_label']=$label;
        }

        return response()->json(['level' => 'success','message' => '设置成功','data' => $result]);
    }





    public function getResume(Request $request){

        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录！',"data"=>"no_login"]);

        $thatResume = Resume::whereUserId($user->id)->first();

        if (!$thatResume)
           return response()->json(['level' => 'error','message' => '未填写个人简历！']);

        $thatUserInfo = Userinfo::whereUserId($user->id)->first();
        if(!$thatUserInfo)
            return response()->json(['level' => 'error','message' => '用户数据错误！']);

        $thatUserInfo->photo_url = FileManager::getImgFromOssUrl($thatUserInfo->photo_id);

        $thatJobType=JobType::whereId($thatResume->job_type_id)->first();

        $jobName = $thatJobType ? $thatJobType->name : "无";

        $thatResume->user_info = $thatUserInfo;
        $thatResume->job_name = $jobName;

        
        return response()->json(['level' => 'success','message' => '获取成功','data' =>$thatResume]);
    }

    public function getReceivedResumeList(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录！',"data"=>"no_login"]);

        $positions  = Position::whereUserId($user->id)->get();
        $resumeIdArr = array();
        if(count($positions) > 0){
            foreach ($positions as $position){
                /** @var PositonResume $receiveResume */
                $receiveResumes = PositonResume::wherePositionId($position->id)->get();
                if(count($receiveResumes) > 0){
                    foreach ($receiveResumes as $receiveResume){
                        /** @var PositonResume $receiveResume */
                        array_push($resumeIdArr,$receiveResume->resume_id);
                    }
                }
            }
        }

        $resumes = Resume::whereIn("id",$resumeIdArr)->get();
        if(count($resumes) > 0){
            foreach ($resumes as $resume){
                /** @var Resume $resume */
                $thePosition = DB::table('position')
                    ->leftJoin('position_resumes', 'position_resumes.position_id', '=', 'position.id')
                    ->leftJoin('resume','resume.id','=','position_resumes.resume_id')
                    ->where('resume.id','=',$resume->id)->whereNull("position.deleted_at")
                    ->select('position.*')
                    ->first();

                if($thePosition){
                    $resume->position = $thePosition;

                }else{
                    $resume->position = null;
                }

                /** @var JobType $joyType */
                $jobType = JobType::whereId($resume->job_type_id)->first();
                if($jobType){
                    $resume->job_type = $jobType->name;
                }else{
                    $resume->job_type = null;
                }
                /** @var @ $jobTypePosition */
                $jobTypePosition = JobType::whereId($thePosition->job_type_id)->first();
                if($thePosition){
                    $thePosition->job_type_position = $jobTypePosition->name;
                }else{
                    $thePosition->job_type_position = null;
                }


                $userInfo = Userinfo::whereUserId($resume->user_id)->first();
                if($userInfo){
                    $userInfo->photo_url = FileManager::getAliOssFile($userInfo->photo_id);
                    $resume->user_info = $userInfo;
                }else{
                    $resume->user_info = null;
                }
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' =>$resumes]);
    }

    public function sendResume(Request $request){
        $positionId = $request->get('id');

        /** @var User $user */
        $user = UserManager::isLogin($request);
        if (!$user)
            return response()->json(['level'=>'error','message'=>'请先登录',"data"=>"no_login"]);

        $thatResume= Resume::whereUserId($user->id)->first();
        if (!$thatResume)
            return response()->json(['level' => 'error','message' => '请先填写个人简历！',"data"=>"not_found_resume"]);

        /** @var Position $thatPosition */
        $thatPosition = Position::whereId($positionId)->first();
        if(!$thatPosition)
            return response()->json(['level'=>'error','message'=>'当前职位不存在或已删除！']);

        /** @var PositonResume $sendResume */
        $sendResume=PositonResume::whereResumeId($thatResume->id)->wherePositionId($positionId)->first();
        if($sendResume)
            return response()->json(['level'=>'error','message'=>'你已经投递过简历！']);

        $sendResume = new PositonResume();
        $sendResume->position_id=$positionId;
        $sendResume->resume_id=$thatResume->id;
        $sendResume->save();

        return response()->json(['level' => 'success','message' => '简历投递成功',"data"=> $sendResume]);
    }

    public function setUserFollow(Request $request){
        $fUserId = $request->get("f_user_id");

        /** @var User $user */
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录！',"data"=>"no_login"]);

        if($user->id == $fUserId)
            return response()->json(['level' => 'error','message' => '不能关注自己哦！']);

        /** @var User $thatUser */
        $thatUser = User::whereId($fUserId)->first();
        if(!$thatUser)
            return response()->json(['level' => 'error','message' => '当前用户不存在！']);

        /** @var UserFollow $hasFollow */
        $hasFollow = UserFollow::whereUserId($user->id)->whereFUserId($thatUser->id)->first();
        if(!$hasFollow){
            $hasFollow = new UserFollow();
            $hasFollow->user_id = $user->id;
            $hasFollow->f_user_id = $thatUser->id;
            $hasFollow->save();

            return response()->json(['level' => 'success','message' => '关注成功！',"data"=>"follow"]);
        }else{
            $hasFollow->delete();

            return response()->json(['level' => 'success','message' => '取消关注！',"data"=>"close_follow"]);
        }
    }

    /**
     * 删除发布的招聘信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delMyPosition(Request $request){
        $positionId = $request->get("id");

        /** @var User $user */
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录！',"data"=>"no_login"]);

        /** @var Position $thatPosition */
        $thatPosition = Position::whereId($positionId)->whereUserId($user->id)->first();
        if(!$thatPosition)
            return response()->json(['level' => 'error','message' => '当前id不存在！']);

        $thatPosition->delete();

        return response()->json(['level' => 'success','message' => '删除成功！',"data"=>$thatPosition]);
    }

    public function getUserFollowPositionList(Request $request){
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $userFollows = UserFollow::whereUserId($user->id)->get();
        $positions = array();
        if(count($userFollows) > 0){
            foreach($userFollows as $userFollow){
                $position = Position::whereUserId($userFollow->f_user_id)->first();
                if($position) {
                    $positionImgs = PositionImg::wherePositionId($position->id)->get();
                    if (count($positionImgs) > 0) {
                        foreach ($positionImgs as $positionImg) {
                            $positionImgUrl = FileManager::getImgFromOssUrl($positionImg->img_id);
                            $positionImg->img_url = $positionImgUrl;
                        }
                    }

                    switch ($position->type) {
                        case 0:
                            $position->type = "全职";
                            break;
                        case 1:
                            $position->type = "兼职";
                            break;
                        case 2:
                            $position->type = "实习";
                            break;
                    }

                    $jobType = JobType::whereId($position->job_type_id)->first();
                    if ($jobType) {
                        $position->job_type = $jobType->name;
                    } else {
                        $position->job_type = "无";
                    }

                    $userInfo = UserInfo::whereUserId($position->user_id)->first();
                    if (!$userInfo)
                        return response()->json(['level' => 'error', 'message' => '用户信息不完整']);

                    $announcer = 'personal';
                    if ($userInfo->company_auth == 1) {
                        $announcer = 'company';
                        $company = array();
                        $companyInfo = CompanyInfo::whereUserId($position->user_id)->first();
                        if ($companyInfo) {
                            $trade = Trade::whereId($position->trade_id)->first();
                            if ($trade) {
                                $company['trade_name'] = $trade->name;
                            } else {
                                $company['trade_name'] = '无';
                            }
                            $companyInfo->photo_url = FileManager::getAliOssFile($companyInfo->photo_id);
                            $trade2 = Trade::whereId($companyInfo->industry_id)->first();
                            $companyInfo->industry = $trade2 ? $trade2->name : 0;
                            $company['company_info'] = $companyInfo;
                        } else {
                            $company['company_info'] = null;
                            $company['trade_name'] = '无';
                        }
                        $userInfo->photo_url = FileManager::getAliOssFile($userInfo->photo_id);
                        $company['user_info'] = $userInfo;
                        $company["user_type"] = $announcer;
                        $position->info = $company;
                    } else {
                        $personal = array();
                        $companyInfo = CompanyInfo::whereUserId($position->user_id)->first();
                        if ($companyInfo) {
                            $trade3 = Trade::whereId($companyInfo->industry_id)->first();
                            $companyInfo->industry = $trade3 ? $trade3->name : 0;
                            $companyInfo->photo_url = FileManager::getAliOssFile($companyInfo->photo_id);
                            $personal['company_info'] = $companyInfo;
                        } else {
                            $personal['company_info'] = null;
                        }

                        $userInfo->photo_url = FileManager::getAliOssFile($userInfo->photo_id);
                        $personal['user_info'] = $userInfo;

                        $thatTrade = Trade::whereId($position->trade_id)->first();
                        $personal['trade_name'] = $thatTrade ? $thatTrade->name : '无';

                        $personal["user_type"] = $announcer;
                        $position->info = $personal;
                    }
                    $position->position_img = $positionImgs;

                    $positionTrade = Trade::whereId($position->trade_id)->first();
                    $position->trade_name = $positionTrade ? $positionTrade->name : '';

                    $user = UserManager::isLogin($request);
                    if ($user) {
                        $hasFollow = UserFollow::whereUserId($user->id)->whereFUserId($position->user_id)->first();
                        $position->is_follow = $hasFollow ? 1 : 0;
                    } else {
                        $position->is_follow = 0;
                    }

                    array_push($positions,$position);
                }
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $positions]);
    }
}
